
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import {sumStatisticsAction, sumStatisticsState} from './index';
import moment from 'moment';
import { Table } from 'antd';
import SearchList from './SeachList/SearchList';
import less from './SumStatistics.less';
import { axios } from 'utils';
import download from 'downloadjs';
import TextArea from 'antd/lib/input/TextArea';

class SumStatistics extends Component{
    constructor(props) {
        super(props);
        this.state = {
            initTime: [moment(), moment()]
        };
        this.columns = [
            { title: '放款日期', dataIndex: 'day', key: 'day', width: 150 },
            { title: '放款笔数', dataIndex: 'allCount', key: 'allCount', width: 150 },
            { title: '老客笔数', dataIndex: 'olderCount', key: 'olderCount', width: 150 },
            { title: '新客笔数', dataIndex: 'newerCount', key: 'newerCount', width: 150 },
            { title: '放款本金', dataIndex: 'loanMoney', key: 'loanMoney', width: 150 },
            { title: '正常应还金额', dataIndex: 'allDeviceMoney', key: 'allDeviceMoney', width: 150 },
            { title: '老客正常应还金额', dataIndex: 'olderDeviceMoney', key: 'olderDeviceMoney', width: 150 },
            { title: '新客正常应还金额', dataIndex: 'newerDeviceMoney', key: 'newerDeviceMoney', width: 150 },
            { title: '老客放款金额', dataIndex: 'olderLoanMoney', key: 'olderLoanMoney', width: 150 },
            { title: '新客放款金额', dataIndex: 'newerLoanMoney', key: 'newerLoanMoney', width: 150 },
            { title: '总首逾正常应还金额', dataIndex: 'allOverdueMoney', key: 'allOverdueMoney', width: 150 },
            { title: '老客首逾正常应还金额', dataIndex: 'olderOverdueDeviceMoney', key: 'olderOverdueDeviceMoney', width: 150 },
            { title: '新客首逾正常应还金额', dataIndex: 'newerOverdueDeviceMoney', key: 'newerOverdueDeviceMoney', width: 150 },
            { title: '总首逾率', dataIndex: 'allOverdueMoneyRate', key: 'allOverdueMoneyRate', width: 150 },
            { title: '老客首逾率', dataIndex: 'olderOverdueDeviceMoneyRate', key: 'olderOverdueDeviceMoneyRate', width: 150 },
            { title: '新客首逾率', dataIndex: 'newerOverdueDeviceMoneyRate', key: 'newerOverdueDeviceMoneyRate', width: 150 },
            { title: '总在逾正常应还金额', dataIndex: 'currentOverdueMoney', key: 'currentOverdueMoney', width: 150 },
            { title: '老客在逾金额', dataIndex: 'olderCurrentOverdueMoney', key: 'olderCurrentOverdueMoney', width: 150 },
            { title: '新客在逾金额', dataIndex: 'newerCurrentOverdueMoney', key: 'newerCurrentOverdueMoney', width: 150 },
            { title: '总在逾率', dataIndex: 'currentOverdueMoneyRate', key: 'currentOverdueMoneyRate', width: 150 },
            { title: '老客在逾率', dataIndex: 'olderCurrentOverdueMoneyRate', key: 'olderCurrentOverdueMoneyRate', width: 150 },
            { title: '新客在逾率', dataIndex: 'newerCurrentOverdueMoneyRate', key: 'newerCurrentOverdueMoneyRate', width: 150 },
            { title: '总催回金额', dataIndex: 'urgeDeviceMoney', key: 'urgeDeviceMoney', width: 150 },
            { title: '老客催回金额', dataIndex: 'olderUrgeBackDeviceMoney', key: 'olderUrgeBackDeviceMoney', width: 150 },
            { title: '新客催回金额', dataIndex: 'newerUrgeBackDeviceMoney', key: 'newerUrgeBackDeviceMoney', width: 150 },
            { title: '总催回率', dataIndex: 'urgeDeviceMoneyRate', key: 'urgeDeviceMoneyRate', width: 150 },
            { title: '老客催回率', dataIndex: 'olderUrgeBackDeviceMoneyRate', key: 'olderUrgeBackDeviceMoneyRate', width: 150 },
            { title: '新客催回率', dataIndex: 'newerUrgeBackDeviceMoneyRate', key: 'newerUrgeBackDeviceMoneyRate', width: 150 },
            { title: '总还本金', dataIndex: 'backLoanMoney', key: 'backLoanMoney', width: 150 },
            { title: '总逾期费', dataIndex: 'delayedMoney', key: 'delayedMoney', width: 150 },
            { title: '总还款金额', dataIndex: 'backTotalMoney', key: 'backTotalMoney', width: 150 },
            { title: '老客还款金额', dataIndex: 'olderTotalMoney', key: 'olderTotalMoney', width: 150 },
            { title: '新客还款金额', dataIndex: 'newerTotalMoney', key: 'newerTotalMoney', width: 150 },
            { title: '总回款率', dataIndex: 'backTotalMoneyRate', key: 'backTotalMoneyRate', width: 150 },
            { title: '老客回款率', dataIndex: 'olderTotalMoneyRate', key: 'olderTotalMoneyRate', width: 150 },
            { title: '新客回款率', dataIndex: 'newerTotalMoneyRate', key: 'newerTotalMoneyRate', width: 150 },
        ];
    }

    getSourceData = (data) => {
        axios({
            url: '/hs/admin/channel/getChannelList',
            method: 'post',
            data: data
        }).then((res) => {
            if(res.code === 200){
                this.setState({
                    sourceData:res.data.records
                })
            }
        })
    }

    getTableData = (data) => {
        console.log(data)
        axios({
            url: '/hs/admin/statistics/summaryStatistic',
            method: 'post',
            data: data
        }).then((res) => {
            if(res.code === 200){  
                this.setState({
                    tableData:res.data
                })
            }
        })
    }

    convertParams = (time, channelId) => {
        const isArr = Array.isArray(time) && time.length > 0;
        return {
            startTime: isArr ? time[0].format('YYYY-MM-DD 00:00:00') : '',
            endTime: isArr ? time[1].format('YYYY-MM-DD 23:59:59') : '',
            channelId: (channelId === 0 ) ? '0' : channelId ? channelId : ''
        };
    }
    handleSearch = (obj) => {
        const { time, channelId } = obj;
        const params = this.convertParams(time, channelId);
        this.getTableData({ ...params });
    }

    handlReturn = (obj) => {
        const { time, channelId } = obj;
        const params = this.convertParams(time, channelId);
        axios({
            url: '/hs/admin/statistics/summaryStatisticReport',
            method: 'post',
            responseType: 'blob',
            data: params
        }).then((res) => {
            download(res, `汇总统计${Date.now()}.xls`);
        }).catch((err) => {
            console.log(err)
        });
    }

    componentDidMount() {
        const params = this.convertParams(this.state.initTime)
        this.getTableData({ ...params });
        this.getSourceData({ pageSize: 10000, pageNum: 1 });
    }

    render() {
        const { loading } = this.props;
        const { tableData, sourceData, initTime } = this.state;
        return (
            <div>
                <SearchList handleSearch={this.handleSearch} handlReturn={this.handlReturn} initTime={initTime} sourceData={sourceData}/>
                <div className={less.tableTop}>
                    <Table columns={this.columns} dataSource={tableData} loading={loading} pagination={false} bordered scroll={{ x: '100%' }} />
                </div>
            </div>
        );

    }
}

const mapStateToProps = (state) => {
    const { businessStatisticsState: { sumStatisticsState } } = state;
    return {
        tableData: sumStatisticsState['tableData'],
        loading: sumStatisticsState['loading']
    };
}
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        getTableData: sumStatisticsAction.assGetTableData,
        setTableData: sumStatisticsAction.assSetTableData
    }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(SumStatistics);